#部署前版本号
hostimg=${HOSTIMG}
depvis=$(kubectl get deploy gosrv-deployment -o go-template='{{index .metadata.annotations "deployment.kubernetes.io/revision"}}')
echo "部署前版本号：${depvis}"
echo ""
sed -i "s, {{image}}, ${hostimg} ,g" go-demo/deployment/service/service.yaml
kubectl apply -f go-demo/deployment/service/service.yaml
cat go-demo/deployment/service/service.yaml

#健康检查
success=0
count=60
IFS=","
#改为休息60秒
#sleep 10
while [ ${count} -gt 0 ]; do
  #判断当前版本号是否大于部署前版本号
  if [ $(kubectl get deploy gosrv-deployment -o go-template='{{index .metadata.annotations "deployment.kubernetes.io/revision"}}') -lt ${depvis} ]; then
    ((count--))
    sleep 2
    continue
  fi
  #执行检查
  replicas=$(kubectl get deploy gosrv-deployment -o go-template='{{.status.replicas}},{{.status.updatedReplicas}},{{.status.availableReplicas}}')
  echo "replicas: ${replicas}"
  echo "${arr[0]} ${arr[1]}  ${arr[1]}";
  arr=(${replicas})
   if [ "${arr[0]}" == "${arr[1]}" -a "${arr[1]}" == "${arr[2]}" ];then
    echo "health check success!"
    success=1
    break
  fi
  ((count--))
  sleep 2
done
if [ ${success} -ne 1 ]; then
  echo "health check failed!"
  exit 1
fi
